package com.rongke.damo.medicalrecord.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.biz.service.MedicalRecordManager;
import com.rongke.biz.service.UserManager;
import com.rongke.commons.JsonResp;
import com.rongke.model.MedicalRecords;
import com.rongke.model.OperationRecord;
import com.rongke.model.PathologyReport;
import com.rongke.model.User;
import com.rongke.service.PathologyReportService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jodd.util.StringUtil;

/**
 * 术后病理报告
 * 
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/damo/pathologyReport")
@Transactional
public class DamoPathologyReportController {
    private static final Logger    LOGGER = Logger.getLogger(DamoPathologyReportController.class);

    @Autowired
    private PathologyReportService pathologyReportService;

    @Autowired
    private UserManager            userManager;

    @Autowired
    private MedicalRecordManager   medicalRecordManager;

    /**
     * @添加手术病理报告
     * @param pathologyReport
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JsonResp addPathologyReport(@RequestBody Map<String, String> jsonMap) {
        LOGGER.debug("addPathologyReport");

        try {
            String mobileNo = jsonMap.get("phone");
            if (StringUtil.isEmpty(mobileNo)) {
                return JsonResp.fa(null);
            }

            User user = userManager.selectOrAddUser(mobileNo);
            if (null == user) {
                return JsonResp.fa(null);
            }
            String userId = user.getUserId();

            MedicalRecords mrInfo = medicalRecordManager.selectOrAddMrByUserId(userId);
            if (null == mrInfo) {
                return JsonResp.fa(null);
            }
            long mrId = mrInfo.getId();

            PathologyReport prInfo = new PathologyReport();
            prInfo.setMedicalRecordId(mrId);
            prInfo.setUserId(userId);
            prInfo.setDate(jsonMap.get("date"));
            prInfo.setPicContent(jsonMap.get("picContent"));
            prInfo.setTagInfo(jsonMap.get("tagInfo"));

            pathologyReportService.insert(prInfo);

            JSONObject json = new JSONObject();
            json.put("id", prInfo.getId());
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("addPathologyReport failed", e);
            return JsonResp.fa(null);
        }
    }

    /**
     * @修改手术病理报告
     * @param pathologyReport
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updatePathologyReport(@RequestBody PathologyReport pathologyReport) {
        LOGGER.debug("updatePathologyReport");

        try {
            PathologyReport prInfo = new PathologyReport();
            prInfo.setId(pathologyReport.getId());
            prInfo.setDate(pathologyReport.getDate());
            prInfo.setPicContent(pathologyReport.getPicContent());
            prInfo.setTagInfo(pathologyReport.getTagInfo());

            pathologyReportService.updateById(prInfo);

            JSONObject json = new JSONObject();
            json.put("id", pathologyReport.getId());
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("updatePathologyReport failed", e);
            return JsonResp.fa(null);
        }
    }

    /**
     * 根据ID删除检查记录
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public JsonResp delPathologyReportById(long id) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("delPathologyReportById, id=" + id);
        }

        try {
            pathologyReportService.deleteById(id);

            JSONObject json = new JSONObject();
            json.put("id", id);
            return JsonResp.ok(json);
        } catch (Exception e) {
            LOGGER.error("delPathologyReportById failed, id=" + id, e);
            return JsonResp.fa(null);
        }
    }

    /**
     * @根据id查找手术病理报告
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectPathologyReport(Long id) {
        LOGGER.debug("selectPathologyReport");

        try {
            PathologyReport pathologyReport = pathologyReportService.selectById(id);
            return JsonResp.ok(pathologyReport);
        } catch (Exception e) {
            LOGGER.error("selectPathologyReport failed, id=" + id, e);
            return JsonResp.fa(null);
        }
    }

    @RequestMapping(value = "/selectAllByPhone", method = RequestMethod.GET)
    public JsonResp selectList(String userId) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("selectList, userId=" + userId);
        }

        try {
            MedicalRecords mr = medicalRecordManager.selectByUserId(userId);
            if (null == mr) {
                return JsonResp.ok();
            }

            long mrId = mr.getId();

            EntityWrapper<PathologyReport> ew = new EntityWrapper<PathologyReport>();
            ew.eq("medical_record_id", mrId);
            ew.orderBy("date");
            List<PathologyReport> result = pathologyReportService.selectList(ew);
            return JsonResp.ok(result);
        } catch (Exception e) {
            LOGGER.error("selectList failed, userId=" + userId, e);
            return JsonResp.fa(null);
        }
    }
}
